#include "sessiondetailwidget.h"
#include "debug.h"

/****************************
 * 表示一个头像 +  一个名字组合控件
 ***************************/

AvatarItem::AvatarItem(const QString &name, const QIcon &icon)
{
    // 1. 设置基本属性
    this->setFixedSize(70,80);

    // 2. 创建布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,10,0,0);
    vlayout->setAlignment(Qt::AlignHCenter);
    this->setLayout(vlayout);

    // 3. 创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(45,45);
    avatarBtn->setIconSize(QSize(45,45));
    avatarBtn->setIcon(icon);
    avatarBtn->setStyleSheet("QPushButton { border: none; }");

    // 4. 创建名字
    nameLabel = new QLabel();
    nameLabel->setText(name);
    nameLabel->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    QFont font("宋体",12);
    nameLabel->setFont(font);

    // 5. 对名字做 "截断操作"
    const int MAX_WIDTH = 65;
    QFontMetrics metrics(font);
    int totalWidth = metrics.horizontalAdvance(name);
    if(totalWidth >= MAX_WIDTH)
    {
        // 需要截断
        QString tail = "...";
        int tailWidth = metrics.horizontalAdvance(tail);
        int availableWidth = MAX_WIDTH - tailWidth;
        int availableSize = name.size() * ((double)availableWidth / totalWidth);
        QString newName = name.left(availableSize);
        nameLabel->setText(newName + tail);
    }

    // 6. 添加到布局管理器
    vlayout->addWidget(avatarBtn);
    vlayout->addWidget(nameLabel);
}

/****************************
 * 表示 "单聊会话详情" 窗口
 ***************************/
sessionDetailWidget::sessionDetailWidget(QWidget *parent, const UserInfo &userInfo)
    : QDialog(parent),userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedSize(200,200);
    this->setWindowFlag(Qt::WindowType::FramelessWindowHint);
    // 窗口被关闭时, 自动销毁这个对话框对象.
    this->setAttribute(Qt::WA_DeleteOnClose);
    // 把窗口移动到鼠标当前的位置
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    QGridLayout* glatyout = new QGridLayout();
    glatyout->setSpacing(0);
    glatyout->setContentsMargins(10,10,10,10);
    this->setLayout(glatyout);

    // 3. 添加 "创建群聊" 按钮
    AvatarItem* addItem = new AvatarItem("添加",QIcon(":/resource/image/cross.png"));
    glatyout->addWidget(addItem,0,0);

    // 4. 添加当前用户的信息 (临时构造的假数据)
#ifdef TEST_UI
    AvatarItem* userItem = new AvatarItem(userInfo.nickname,userInfo.avatar);
    glatyout->addWidget(userItem,0,1);
#endif

    // 5. 添加 "删除好友" 按钮
    delFriendBtn = new QPushButton();
    delFriendBtn->setFixedHeight(50);
    delFriendBtn->setText("删除好友");
    QString style = "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; } ";
    style += "QPushButton:pressed { background-color: rgb(235, 235, 235); }";
    delFriendBtn->setStyleSheet(style);
    glatyout->addWidget(delFriendBtn,1,0,1,3);


    // 6. 连接信号槽
    connect(addItem->getAvatarBtn(),&QPushButton::clicked,this,[=](){
        MainWidget* mainwidget = MainWidget::getInstance();
        chooseFriendDialog* choosefrienddialog = new chooseFriendDialog(mainwidget);
        choosefrienddialog->exec();
    });

}

void sessionDetailWidget::leaveEvent(QEvent *event)
{
    (void) event;
    this->close();
    QDialog::leaveEvent(event);
}

